{include file="public/header"}

<body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form action="" method="post" class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span> 备注
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="data[nickname]" value="{$data.nickname|default=''}"
                            required="" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span> 备注，方便快速记录用户
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span> 账号
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="username" name="data[username]" value="{$data.username|default=''}"
                            required="" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span> 管理员登录账号
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span> 密码
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="password" name="data[password]" value="" autocomplete="off"
                            class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>密码若不填写不修改密码
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>管理员类型</label>
                    <div class="layui-input-inline">
                        <select name="data[type]" lay-verify="required" lay-filter="types">
                            <option value="0">选择管理员类型</option>
                            {if $type == 1}<option value="1" {if !empty($data.type) && $data.type==1}selected{/if}>总管理员
                            </option>{/if}
                            {if $type == 1}<option value="2" {if !empty($data.type) && $data.type==2}selected{/if}>城市合伙人
                            </option>{/if}
                            {if $type == 2 || $type == 1}<option value="3" {if !empty($data.type) &&
                                $data.type==3}selected{/if}>区、县合伙人</option>{/if}
                            {if $type == 2 || $type == 1 || $type == 3}<option value="4" {if !empty($data.type) &&
                                $data.type==4}selected{/if}>村、镇合伙人</option>{/if}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>选择省</label>
                    <div class="layui-input-inline">
                        <select name="data[province]" data-area="" lay-filter="province">
                            <option value="">选择省</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>选择市</label>
                    <div class="layui-input-inline">
                        <select name="data[city]" data-area="" lay-filter="city">
                            <option value="">选择市</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>选择区域</label>
                    <div class="layui-input-inline">
                        <div id="areaid"></div>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">
                        拥有权限
                    </label>
                    <table class="layui-table layui-input-block">
                        <tbody>
                            {foreach :config('menu.menu') as $k => $v}
                            {if $v.show == true && $type == 1}
                            <tr>
                                <td>
                                    <input type="checkbox" name="like1[write]" lay-skin="primary" lay-filter="father"
                                        title="{$v.name}">
                                </td>
                                <td>
                                    <div class="layui-input-block">
                                        {foreach $v.sub as $x => $y}
                                        <input name="data[role][]" lay-skin="primary" type="checkbox" value="{$y.uri}"
                                            title="{$y.name}" {if isset($data.role) && in_array($y.uri, $data.role)}
                                            checked {/if}>
                                        {/foreach}
                                    </div>
                                </td>
                            </tr>
                            {/if}
                            {if $v.show == true && $type != 1}
                            <tr>
                                <td>
                                    <input type="checkbox" name="like1[write]" lay-skin="primary" lay-filter="father"
                                        title="{$v.name}">
                                </td>
                                <td>
                                    <div class="layui-input-block">
                                        {foreach $v.sub as $x => $y}
                                        {if isset($role) && in_array($y.uri, $role)}
                                        <input name="data[role][]" lay-skin="primary" type="checkbox" value="{$y.uri}"
                                            title="{$y.name}" {if isset($data.role) && in_array($y.uri, $data.role)}
                                            checked {/if}>
                                        {/if}
                                        {/foreach}
                                    </div>
                                </td>
                            </tr>
                            {/if}
                            {/foreach}
                        </tbody>
                    </table>
                </div>
                <div class="layui-form-item">
                    <input type="hidden" value="{$data['id']|default='0'}" name="data[id]" />
                    <button class="layui-btn" lay-submit="" lay-filter="add">提交</button>
                </div>
            </form>
        </div>
    </div>
    {include file="public/footer"}

    <script>
        layui.use(['form', 'layer'], function () {
            $ = layui.jquery;
            var form = layui.form
                , layer = layui.layer;
            //渲染省份
            $.ajax({
                type: 'get',
                url: '/base/getarea2',
                data: { level: 1, adminid: "{$data['id']|default='0'}" },
                success: function (res) {
                    if (res.code == 0) {
                        var str = '<option value="">请选择省份</option>';
                        $.each(res.data, function (i, obj) {
                            str += '<option value="' + obj.area_code;
                            if (res.type != 1) {
                                if (obj.area_code == res.province) {
                                    str += '" selected>' + obj.name;
                                } else {
                                    str += '" disabled>' + obj.name;
                                }
                            } else {
                                if (obj.area_code == res.province) {
                                    str += '" selected>' + obj.name;
                                } else {
                                    str += '">' + obj.name;
                                }
                            }
                            str += '</option>';
                        });
                        $('select[name="data[province]"]').html(str);
                        form.render();
                    }
                }
            });
            //选软城市
            $.ajax({
                type: 'get',
                url: '/base/getarea2',
                data: { level: 2, adminid: "{$data['id']|default='0'}" },
                success: function (res) {
                    if (res.code == 0) {
                        var str = '<option value="">请选择城市</option>';
                        $.each(res.data, function (i, obj) {
                            str += '<option value="' + obj.area_code;
                            if (res.type != 1) {
                                if (obj.area_code == res.city) {
                                    str += '" selected>' + obj.name;
                                } else {
                                    str += '" disabled>' + obj.name;
                                }
                            } else {
                                if (obj.area_code == res.city) {
                                    str += '" selected>' + obj.name;
                                } else {
                                    str += '">' + obj.name;
                                }
                            }
                            str += '</option>';
                        });
                        $('select[name="data[city]"]').html(str);
                        form.render();
                    }
                }
            });
            //选软区域
            $.ajax({
                type: 'get',
                url: '/base/getarea2',
                data: { level: 3, adminid: "{$data['id']|default='0'}" },
                success: function (res) {
                    if (res.code == 0) {
                        if (res.type == 2) {
                            var str = '';
                            $.each(res.data, function (i, obj) {
                                str += '<input type="checkbox" name="data[area][]" lay-skin="primary" lay-filter="father" title="' + obj.name + '">';
                            });
                            console.log(str);
                        } else {
                            var str = '<select name="data[area]" data-area="" lay-filter="area"><option value="">请选择区域</option>';
                            $.each(res.data, function (i, obj) {
                                str += '<option value="' + obj.area_code;
                                if (res.type == 3) {
                                    if (obj.area_code == res.area && "{$data['id']|default='0'}" > 0) {
                                        str += '" selected>' + obj.name;
                                    } else {
                                        str += '" disabled>' + obj.name;
                                    }
                                } else {
                                    if (obj.area_code == res.area && "{$data['id']|default='0'}" > 0) {
                                        str += '" selected>' + obj.name;
                                    } else {
                                        str += '">' + obj.name;
                                    }
                                }
                                str += '</option>';
                            });
                            str += '</select >';
                        }
                        $('#areaid').html(str);
                        form.render();
                    }
                }
            });
            //选软区域
            $.ajax({
                type: 'get',
                url: '/base/getarea2',
                data: { level: 4, adminid: "{$data['id']|default='0'}" },
                success: function (res) {
                    if (res.code == 0) {
                        var str = '<option value="">请选择街道</option>';
                        $.each(res.data, function (i, obj) {
                            str += '<option value="' + obj.id;
                            if (res.type == 4) {
                                if (obj.id == res.street && "{$data['id']|default='0'}" > 0) {
                                    str += '" selected>' + obj.name;
                                } else {
                                    str += '" disabled>' + obj.name;
                                }
                            } else {
                                if (obj.area_code == res.area && "{$data['id']|default='0'}" > 0) {
                                    str += '" selected>' + obj.name;
                                } else {
                                    str += '">' + obj.name;
                                }
                            }
                            str += '</option>';
                        });
                        $('select[name="data[street]"]').html(str);
                        form.render();
                    }
                }
            });
            //监听省份选择市区
            form.on('select(province)', function (data) {
                $.ajax({
                    type: 'get',
                    url: '/base/getarea2',
                    data: { id: data.value, level: 2 },
                    success: function (res) {
                        if (res.code == 0 && data.value > 0) {
                            var str = '<option value="">请选择城市</option>';
                            $.each(res.data, function (i, obj) {
                                str += '<option value="' + obj.area_code;
                                str += '">' + obj.name;
                                str += '</option>';
                            });
                            $('select[name="data[city]"]').html(str);
                            form.render();
                        }
                    }
                });
                return false;
            });

            //监听市区选择地区
            form.on('select(city)', function (data) {
                $.ajax({
                    type: 'get',
                    url: '/base/getarea2',
                    data: { id: data.value, level: 3 },
                    success: function (res) {
                        if (res.code == 0 && data.value > 0) {
                            var str = '<option value="">请选择县/区域</option>';
                            $.each(res.data, function (i, obj) {
                                str += '<option value="' + obj.area_code;
                                str += '">' + obj.name;
                                str += '</option>';
                            });
                            $('select[name="data[area]"]').html(str);
                            form.render();
                        }
                    }
                });
                return false;
            });
            //监听市区选择地区
            form.on('select(area)', function (data) {
                $.ajax({
                    type: 'get',
                    url: '/base/getarea2',
                    data: { id: data.value, level: 4 },
                    success: function (res) {
                        if (res.code == 0 && data.value > 0) {
                            var str = '';
                            $.each(res.data, function (i, obj) {
                                str += '<input type="checkbox" name="data[street][' + obj.id + ']" lay-skin="primary" lay-filter="father" title = "' + obj.name + '" > ';
                                // str += '<option value="' + obj.id;
                                // str += '">' + obj.name;
                                // str += '</option>';
                            });
                            $('#streetids').html(str);
                            // $('select[name="data[street]"]').html(str);
                            form.render();
                        }
                    }
                });
                return false;
            });
            //监听提交
            form.on('submit(add)', function (data) {
                //发异步，把数据提交给php
                $.ajax({
                    'type': 'post',
                    data: data.field,
                    success: function (res) {
                        if (res.status == 0) {
                            layer.msg(res.msg, { icon: 6, time: 1000 }, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, { icon: 2, time: 1000 });
                        }
                    }
                });
                return false;
            });

            form.on('checkbox(father)', function (data) {

                if (data.elem.checked) {
                    $(data.elem).parent().siblings('td').find('input').prop("checked", true);
                    form.render();
                } else {
                    $(data.elem).parent().siblings('td').find('input').prop("checked", false);
                    form.render();
                }
            });


        });
    </script>